<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    /*  function fn() {
         var a = 10;  // AO(fn) {a:10}
 
         function addA() {
             a++;
             console.log(a);
             return a;
         }
 
         function reduceA() {
             a--;
             console.log(a);
         }
 
         function getA() {
             return a;
         }
 
         function setA(val) {
             a = val;
         }
 
         return {
             addA,
             reduceA,
             getA,
             setA,
         }
     }
 
 
     var obj = fn();
     console.log(obj);
 
     var { getA, setA, addA, reduceA } = obj;
 
 
     addA();
     addA();
     addA();
     addA();
 
     reduceA()
     reduceA()
     reduceA()
     reduceA()
 
     setA(100);
 
     console.log(getA());
 
 
     addA();
     addA();
     addA();
     addA(); */


</script>
<script>
    function fn() {
        var a = 10;  // AO(fn) {a:10}


        function addA() {
            a++;
            return a;
        }

        function reduceA() {
            a--;
            return a;
        }

        function getA() {
            return a;
        }

        function setA(val) {
            a = val;
        }

        return {
            addA,
            reduceA,

            // 自己封装的取值函数和赋值函数
            getA,
            setA,

            // 给对象新增a属性, 配置取值拦截
            get a() {
                // return a;
                return getA();
            }

        }
    }


    var obj = fn();
    console.log(obj);

    const { getA, setA, addA, reduceA } = obj;
    // var addA = obj.addA;
    // var a = obj.a;

    console.log(obj.a);

    addA();
    addA();
    addA();
    addA();
    addA();

    console.log(obj.a);




</script>

</html>